package com.leet.code.simple_array;

/**
 * @description:
 * @author: WYG
 * @time: 2020/12/25 22:53
 */

public class DistanceBetweenBusStops {

    public int distanceBetweenBusStops(int[] distance, int start, int destination) {
        //通过两次遍历求解最小值,比较顺时针还是逆时针的路程短,选取路程较短的结果作为返回值
        //首先根据start和destination确定起点和终点的大小值
        int a = start > destination ? start : destination;
        int b = start > destination ? destination : start;
        //min以及temp存储顺时针以及逆时针对应的路径
        int min = 0;
        int temp = 0;
        //计算顺时针的路径
        for (int i = b; i < a; i++) {
            min += distance[i];
        }
        //计算逆时针的路径
        for (int i = 0; i < b; i++) {
            temp += distance[i];
        }
        for (int i = a; i < distance.length; i++) {
            temp += distance[i];
        }
        //返回最终的结果
        return min > temp ? temp : min;
    }
}
